<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <div class="top-bar">
          <div class="top-logo">
            <img src="../assets/Verification/pal  logo.png" alt="" />
          </div>
          <div class="top-left">
            <!--<div class="pic1">-->
              <!--<img src="../assets/Verification/question.png" alt="" />-->
            <!--</div>-->
            <!--<div class="pic2">-->
              <!--<img src="../assets/Verification/message.png" alt="" />-->
            <!--</div>-->
            <div class="pic3" style="margin-top: 12px;">
              <el-dropdown>
                <span class="el-dropdown-link">
                  <img v-if="currentInfo.imageHeader" :src="currentInfo.imageHeader"  class="imageHeader">
                  <img v-else src="../assets/images/default-headImg.png" alt="" class="imageHeader">
                  {{currentInfo.userName}} <i class="el-icon-arrow-down el-icon--right"></i>
                </span>
                <el-dropdown-menu slot="dropdown">
                  <!--<el-dropdown-item>个人中心</el-dropdown-item>-->
                  <el-dropdown-item @click.native="logout">退出</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </div>
        </div>
      </div>
      <!-- <el-button type="info" @click="logout">退出</el-button> -->
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" style="padding: 10px 0" @click="isCollapse = !isCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu background-color="#2A3B67" text-color="#fff" text-size="24px" active-text-color="#ffffff" unique-opened
          :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath" @select="handleSelect">
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id" v-if="item.childResources">
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="item.frontCls+' ic-menu-icon'"></i>
              <!-- 文本 -->
              <span slot="title">{{item.resourceName}}</span>
            </template>
            <!-- 二级菜单区域 -->
            <el-menu-item :index="'/' + subItem.resourceUrl" v-for="subItem in item.childResources" :key="subItem.id"
              @click="saveNavState('/'+subItem.resourceUrl)">
              <template slot="title">
                <!-- 图标 -->
                <i :class="subItem.frontCls+' ic-menu-icon'"></i>
                <!-- 文本 -->
                <span>{{ subItem.resourceName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
          <el-menu-item v-else :index="'/' + item.resourceUrl"
                        @click="saveNavState('/'+item.resourceUrl)">
              <!-- 图标 -->
              <i :class="item.frontCls+' ic-menu-icon'"></i>
              <!-- 文本 -->
              <span>{{ item.resourceName }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main :style="{paddingLeft: isCollapse ? '84px' : '220px'}">
        <div class="main-container">
          <!-- 路由占位符 -->
          <router-view></router-view>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menulist: [],
      currentInfo: {},
      imageUrl: '',
      isCollapse: false,
      activePath: '/index'
    }
  },
  methods: {
    logout () {
      this.$http({
        url: '/logout',
        method: 'get',
        params: {}
      }).then(({ data }) => {
        window.sessionStorage.clear()
        this.$router.push('/login')
      })
    },
    // 获取所有的菜单数据
    async getMenuList () {
      const { data: res } = await this.$http.get('tenant/current/resources')
      // console.log(res)
      if (res.code == 0) {
        this.menulist = res.data
        // this.reload() 刷新页面的数据
      } else {

      }
      // console.log(res, 1111)
      // this.reload()
    },
    //获取当前登录用户的信息
    getCurrentInfo (){
      this.$http({
        url: '/tenant/current/user',
        method: 'get',
        params:{}
      }).then(({data}) => {
          if(data.code == 0){
              this.currentInfo = data.data
          }
      })
    },
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath // 从本地回话存储中取出
    },

    handleSelect (index) {
      this.activePath = index
    }
  },
  watch: {
    $route: {
      handler: function(val, oldVal){
        console.log(val);
        this.handleSelect(val.path)
      },
      // 深度观察监听
      deep: true
    }
  },
  created () {
    // 在生命周期钩子中获取所有数据
    this.getMenuList()
    this.getCurrentInfo()
    var activePath = window.sessionStorage.getItem('activePath')
    if(activePath){
      this.activePath = activePath
    }
  },
  mounted () {
    // this.getMenuList()
  },
  inject: ['reload']
}
</script>

<style lang="less" scoped>
  .imageHeader{
    width: 35px;
    height: 35px;
    border-radius: 100px;
  }
/*S=整体布局样式*/
.home-container {
  .el-container {
    width: 100%;
  }
  .container {
    background-color: #fff;
    padding: 20px;
  }
}
/*E=整体布局样式*/
/* 头部样式开始 */
.top-bar {
  width: 100%;
  height: 60px;
  position: fixed;
  z-index: 10;
  background: #ffffff;
  box-shadow: 0px 0px 16px 0px rgba(74, 74, 74, 0.5);
  .top-logo {
    padding-top: 8px;
    margin-left: 20px;
  }
  .top-left {
    width: 200px;
    height: 60px;
    float: right;
    margin-right: 60px;
    margin-top: -55px;
    .pic1 {
      float: left;
      width: 30px;
      height: 30px;
      margin-top: 14px;
      cursor: pointer;
    }
    .pic2 {
      float: left;
      width: 30px;
      height: 30px;
      margin-top: 14px;
      margin-left: 30px;
      cursor: pointer;
    }
    .pic3 {
      float: left;
      width: 128px;
      height: 30px;
      margin-top: -30px;
      margin-left: 100px;
      cursor: pointer;
      img {
        vertical-align: middle;
      }
    }
  }
}

.home-container {
  height: 100%;
}
.el-header {
  background-color: #ffffff;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
/* .el-submenu__title {
    font-size: 18px!important;

  } */

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}
.el-button {
  background-color: #9370dd;
}
</style>
<style lang="less">
  @mainColor: #2A3B67;
  @menuActiveColor: #328EDB;
  .el-aside{
    background-color: @mainColor;
    position: fixed;
    height: 100%;
    left: 0;
    .ic-menu-icon{
      color: #ffffff;
    }
    .toggle-button {
      background-color: @mainColor;
      font-size: 10px;
      line-height: 24px;
      color: #fff;
      text-align: center;
      letter-spacing: 0.2em;
      cursor: pointer;
    }
    .el-menu {
      border-right: none;
      font-size: 25px;
    }
    .el-submenu.is-opened>.el-submenu__title .el-submenu__icon-arrow,
    .el-submenu__icon-arrow{
      color: #ffffff;
    }
    .el-menu-item i{
      color: #ffffff;
    }
    .el-menu-item.is-active{
      background-color: @menuActiveColor!important;
    }
  }
</style>
